package com.ncs.dao.impl;

import com.ncs.dao.ProductDao;
import com.ncs.page.PageResult;
import com.ncs.pojo.Product;
import com.ncs.query.ProductQuery;
import com.ncs.util.MybatisUtil;
import com.ncs.vo.ProductVo;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author yinyin
 * @create 2021/07/14 下午 7:39
 */
public class ProductDaoImpl implements ProductDao {
    SqlSession session=null;
    List<Product> products= new ArrayList<>();
    Product product;
    @Override
    public void save(Product product) {
        //获取session
        try {
            session = MybatisUtil.getSession();
            session.insert("com.ncs.pojo.ProductMapper.save", product);
            session.commit();
        }catch(Exception e){
            session.rollback();
            e.printStackTrace();
        }finally {
            MybatisUtil.closeSession(session);
        }

    }
    @Override
    public void update(Product product) {
        try {
            session = MybatisUtil.getSession();
            session.update("com.ncs.pojo.ProductMapper.update", product);
            session.commit();
        }catch(Exception e){
            session.rollback();
            e.printStackTrace();
        }finally {
            MybatisUtil.closeSession(session);
        }
    }

    @Override
    public void delete(Long id) {
        try {
            session = MybatisUtil.getSession();
            session.delete("com.ncs.pojo.ProductMapper.delete", id);
            session.commit();
        }catch(Exception e){
            session.rollback();
            e.printStackTrace();
        }finally {
            MybatisUtil.closeSession(session);
        }
    }

    @Override
    public Product findOne(Long id) {

        //获取session
        try {
            session = MybatisUtil.getSession();
            Product product = session.selectOne("com.ncs.pojo.ProductMapper.findOne", id);

            return product;
        }catch(Exception e){
           e.printStackTrace();
        }finally {
            MybatisUtil.closeSession(session);
        }
        return null;
    }

    @Override
    public List<Product> findAll() {

        //获取session
        try {
            session = MybatisUtil.getSession();
            products = session.selectList("com.ncs.pojo.ProductMapper.findAll");

        }catch(Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtil.closeSession(session);
        }
        return products ;
    }

    @Override
    public List<Product> query(ProductQuery productQuery) {
        try {
            session = MybatisUtil.getSession();
            List<Product> list = session.selectList("com.ncs.pojo.ProductMapper.query",productQuery);
            return list;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtil.closeSession(session);
        }
        return null;
    }
    @Override
    public PageResult queryPage(ProductQuery productQuery) {
        try {
            session = MybatisUtil.getSession();
            Integer totalCount = session.selectOne("com.ncs.pojo.ProductMapper.count",productQuery);
            List<Product> list = session.selectList("com.ncs.pojo.ProductMapper.query", productQuery);
            return new PageResult(totalCount, productQuery.getCurrentPage(), productQuery.getPageSize(),list);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtil.closeSession(session);
        }
        return null;
    }

    @Override
    public Product findByName(ProductVo productVo) {
        //获取session
        try {
            session = MybatisUtil.getSession();
            product = session.selectOne("com.ncs.pojo.ProductMapper.findByName", productVo);

        }catch(Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtil.closeSession(session);
        }
        return product;
    }

    @Override
    public Product findByMap(Map<String, Object> map) {
        try {
            session = MybatisUtil.getSession();
             product = session.selectOne("com.ncs.pojo.ProductMapper.findByMap", map);

        }catch(Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtil.closeSession(session);
        }
        return product;
    }
}
